clear all 
set more off 
set maxvar 15000 
clear matrix
    
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    
    keep if decade==1910 | decade==1940
    gen later = decade==1940 
    gen early = decade==1910 
    
    gen interaction_late = log_father_baseline * later
    gen interaction_early = log_father_baseline * early
    
    file open Table using "$Mydirectory2/main_figures_tables/table3.tex", write replace

    file write Table "\begin{tabularx}{1.05\hsize}{@{\hskip\tabcolsep\extracolsep{5pt}} l c c c c c c }" _n
    file write Table "\toprule" _n
    file write Table "\addlinespace[1ex]" _n
    file write Table "& \multicolumn{3}{c}{IGE} & \multicolumn{3}{c}{Rank-rank}  \\" _n
    file write Table "\addlinespace[1ex]" _n
    file write Table "\cline{2-4} \cline{5-7}"
    file write Table "\addlinespace[2ex]" _n    
    file write Table "& Ratio & Difference & \shortstack{Racial \\ Convergence} & Ratio & Difference & \shortstack{Racial \\ Convergence} \\" _n
    file write Table "& (1) & (2) & (3) & (4) & (5) & (6) \\" _n
    file write Table "\addlinespace[1ex]" _n
    file write Table "\midrule" _n
    file write Table "\addlinespace[1ex]" _n
        
    local group2 "farmfixCW nofarm occ_only occscore personal_inc nearest_census altwgt"
    local group3 "nowork_dad moms parents"
    local group4 "edu region"
    local group5 "age1 age2" 
    
    foreach version in baseline `group2' `group3' `group4' `group5'  {
    
        preserve 
        
        if "`version'"=="baseline" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_baseline"
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_baseline"
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Baseline"
        }
        
        if "`version'"=="age1" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_baseline"
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_baseline"
            local cond "baseline_sample==1 & age<=45" 
            local weight "wgt_sex_race"
            local title1 "Ages 30--45"
        }
        
        if "`version'"=="age2" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_baseline"
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_baseline"
            local cond "baseline_sample==1 & age>=35" 
            local weight "wgt_sex_race"
            local title1 "Ages 35--50"
        }
        
        if "`version'"=="surveyweights" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_baseline"
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_baseline"
            local cond "baseline_sample==1"
            local weight "weight_center" 
            local title1 "Using survey weights"
        }
        
        if "`version'"=="farmfixCW" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_interpolated_CWfix" 
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_interp_CWfix" 
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Using CW farm fix"
        }
        
        if "`version'"=="nofarm" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_baseline" 
            local son_inc_rank "rank_son_baseline_nonfarm" 
            local father_inc_rank "rank_father_baseline_nonfarm"  
            local cond "nonfarm_sample==1" 
            local weight "wgt_sex_race_nonfarm" 
            local title1 "Dropping farmers"
        }
        
        if "`version'"=="occscore" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_1950occscore" 
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_1950occscore"  
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Using IPUMS occscore"
        }   
        
        if "`version'"=="occ_only" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_byocc_interp" 
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_interp_occ" 
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Only occ. in prediction"
        }   
        
        if "`version'"=="nearest_census" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_closest_census_v2" 
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_closest_census_v2"  
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Using nearest Census"
        }
        
        if "`version'"=="altwgt" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_interp_altwgt" 
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_byr_bys_altwgt"  
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Alt. weights in prediction"
        }
        
        if "`version'"=="personal_inc" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_personalinc_interp" 
            local son_inc_rank "rank_son_baseline"
            local father_inc_rank "rank_father_personalinc"  
            local cond "baseline_sample==1"
            local weight "wgt_sex_race"
            local title1 "Using father's income"
        }
        
        if "`version'"=="nowork_dad" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "father_baseline_plus_notworking" 
            local son_inc_rank "rank_son_father_notworking" 
            local father_inc_rank "rank_father_notworking"  
            local cond "father_notworking_sample==1" 
            local weight "wgt_sex_race_father_notworking"
            local title1 "Includes non-working fathers"
        }
        
        if "`version'"=="moms" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "parent_income" 
            local son_inc_rank "rank_son_parent" 
            local father_inc_rank "rank_parent"  
            local cond "parent_sample==1" 
            local weight "wgt_sex_race_parent"
            local title1 "Using mother's occupation"
        }
        
        if "`version'"=="parents" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "parent_income_all" 
            local son_inc_rank "rank_son_parent_all" 
            local father_inc_rank "rank_parent_all"  
            local cond "parent_all_sample==1" 
            local weight "wgt_sex_race_parent_all"
            local title1 "$+$ Moms \& non-working dads"
            }
        
        if "`version'"=="edu" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_byors_edu_interp" 
            local son_inc_rank "rank_son_edusample" 
            local father_inc_rank "rank_father_byr_bys_edu"  
            local cond "edu_sample==1" 
            local weight "wgt_sex_race_edu" 
            local title1 "Using father's education"
        }
        
        if "`version'"=="region" {
            local son_inc_log "log_son_baseline"
            local father_inc_log "log_father_byregion_interp" 
            local son_inc_rank "rank_son_regionsamp" 
            local father_inc_rank "rank_father_byregion"  
            local cond "region_sample==1" 
            local weight "wgt_sex_race_region" 
            local title1 "Using childhood region"
        }
        
        if "`version'"=="equivalized" {
            local son_inc_log "log_son_baseline_equivalized" 
            local father_inc_log "log_father_equivalized" 
            local son_inc_rank "rank_son_equivalized" 
            local father_inc_rank "rank_father_equivalized" 
            local cond "equivalized_sample==1" 
            local weight "wgt_sex_race_equivalized" 
            local title1 "Using family size"
        }
        
        * Section Headers 
        if "`version'"=="farmfixCW" {
            file write Table "\addlinespace[1ex]" _n
            file write Table "\textit{Alt. parental inc. measures:} & & & \\" _n
            file write Table "\addlinespace[1.5ex]" _n  
        }
        if "`version'"=="nowork_dad" {
            file write Table "\addlinespace[1ex]" _n
            file write Table "\textit{Including missing income:} & & & \\" _n
            file write Table "\addlinespace[1.5ex]" _n  
        }
        if "`version'"=="edu" {
            file write Table "\addlinespace[1ex]" _n
            file write Table "\textit{Additional predictors:} & & & \\" _n
            file write Table "\addlinespace[1.5ex]" _n  
        }
        if "`version'"=="age1" {
            file write Table "\addlinespace[1ex]" _n
            file write Table "\textit{Different age ranges:} & & & \\" _n
            file write Table "\addlinespace[1.5ex]" _n  
        }
        
        file write Table "\quad `title1' "

****************        
* IGE
****************
        replace interaction_late = late * `father_inc_log'
        replace interaction_early = early * `father_inc_log'

        reg `son_inc_log' interaction_early interaction_late early later if `cond' [pw=`weight'], robust 
        
    * 1. Ratio 
        nlcom _b[interaction_late]/_b[interaction_early]
            mat coeff= r(b) 
            local coeff1 = coeff[1,1]
            local number1_1: display %-09.2fc `coeff1'      

            mat var = r(V)
            local se1 = sqrt(var[1,1])
            local number1_2: display %-04.2fc `se1' 
            
        file write Table "& `number1_1' "
        

    * 2. Difference
        reg `son_inc_log' interaction_early interaction_late early later  if `cond' [pw=`weight'], robust 
        
        lincom _b[interaction_early]-_b[interaction_late]
        
        //Percentage point change
        local diff = `r(estimate)'
        local number2_1: display %-09.2fc `diff'        
        
        local se2 = `r(se)'
        local number2_2: display %-04.2fc `se2' 
            
        file write Table "& `number2_1' "   
        
    * 3. Third term components 
        foreach x in 1910 1940 {
        
            sum black if decade==`x' & `cond' [aw=`weight']
            local pb = `r(mean)'
            local pw = 1-`pb'
            
            sum `father_inc_log' if decade==`x' & `cond' [aw=`weight']
            local yp_all = `r(mean)'
            local yp_var = `r(Var)'
            
            sum `son_inc_log' if decade==`x' & `cond' [aw=`weight']
            local yc_all = `r(mean)'
            
            foreach j in 1 2 {
                sum `father_inc_log' if decade==`x' & `cond' & race==`j' [aw=`weight']
                local yp_r`j' = `r(mean)'
                
                sum `son_inc_log' if decade==`x' & `cond' & race==`j' [aw=`weight']
                local yc_r`j' = `r(mean)'
            }
            
            local product = `yp_all'*`yc_all'
            display `product' 
            
            local numerator = (`pw'*`yp_r1'*`yc_r1')+(`pb'*`yp_r2'*`yc_r2') - (`yp_all'*`yc_all')
            display `num'

            local thirdterm_`x' = `numerator' / `yp_var'
            display `thirdterm_`x''
        
        }
        
        local diff = `thirdterm_1910' - `thirdterm_1940'
        local diff2: display %-09.2fc `diff'        
        file write Table "& `diff2' "

****************               
* RANK  
****************        
        
        replace interaction_late = late * `father_inc_rank'
        replace interaction_early = early * `father_inc_rank'
        
        reg `son_inc_rank' interaction_early interaction_late early later  if `cond' [pw=`weight'], robust 
        
    * 1. Ratio 
        nlcom _b[interaction_late]/_b[interaction_early]
            mat coeff= r(b) 
            local coeff1 = coeff[1,1]
            local number4_1: display %-09.2fc `coeff1'      

            mat var = r(V)
            local se1 = sqrt(var[1,1])
            local number4_2: display %-04.2fc `se1' 
            
        file write Table "& `number4_1' "
        

    * 2. Difference 
        reg `son_inc_rank' interaction_early interaction_late early later if `cond' [pw=`weight'], robust 
        
        lincom _b[interaction_early]-_b[interaction_late]
        
        //Percentage point change
        local diff = `r(estimate)'
        local number5_1: display %-09.2fc `diff'        
        
        local se2 = `r(se)'
        local number5_2: display %-04.2fc `se2' 
            
        file write Table "& `number5_1' "   
        
    * 3. Third term components 
        foreach x in 1910 1940 {
        
            sum black if decade==`x' & `cond' [aw=`weight']
            local pb = `r(mean)'
            local pw = 1-`pb'
            
            sum `father_inc_rank' if decade==`x' & `cond' [aw=`weight']
            local yp_all = `r(mean)'
            local yp_var = `r(Var)'
            
            sum `son_inc_rank' if decade==`x' & `cond' [aw=`weight']
            local yc_all = `r(mean)'
            
                foreach j in 1 2 { 
                    sum `father_inc_rank' if decade==`x' & `cond' & race==`j' [aw=`weight']
                    local yp_r`j' = `r(mean)'
                    
                    sum `son_inc_rank' if decade==`x' & `cond' & race==`j' [aw=`weight']
                    local yc_r`j' = `r(mean)'
                }
            
            local product = `yp_all'*`yc_all'
            display `product' 
            
            local numerator = (`pw'*`yp_r1'*`yc_r1')+(`pb'*`yp_r2'*`yc_r2') - (`yp_all'*`yc_all')
            display `num'

            local thirdterm_`x' = `numerator' / `yp_var'
            display `thirdterm_`x''
        
        }
        
        local diff = `thirdterm_1910' - `thirdterm_1940'
        local diff2: display %-09.2fc `diff'        
        file write Table "& `diff2' "
        
        file write Table " \\" _n
        
        file write Table " & (`number1_2') & (`number2_2') & "
        file write Table " & (`number4_2') & (`number5_2') & \\" 
        
        file write Table "\addlinespace[0.5ex]" _n

        restore 
    }
    
    file write Table "\addlinespace[1ex]" _n
    file write Table "\bottomrule" _n
    file write Table "\end{tabularx}" _n
    file close Table
